package hcmus.fit.master.sevensea.utils;

import java.io.FileOutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.Display;
import android.view.WindowManager;

public class GameUtils {
    public static Size getScreenSize(Context ctx) {
        WindowManager wm = (WindowManager) ctx
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        return new Size(display.getWidth(), display.getHeight());
    }

    private static int idx = 0;
    public static Bitmap repleceIntervalColor(Bitmap src, int beReplacedColor,
            int replacedColor) {
        for (int i = 0; i < src.getHeight(); i++) {
            for (int j = 0; j < src.getWidth(); j++) {
                if (src.getPixel(j, i) == beReplacedColor) {
                    src.setPixel(j, i, replacedColor);
                }
            }
        }

        try {
            FileOutputStream out = new FileOutputStream("D:/xxx/hehe"+ idx + ".png");
            src.compress(Bitmap.CompressFormat.PNG, 90, out);
            idx++;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return src;
    }

    /**
     * create a transparent bitmap from an existing bitmap by replacing certain
     * color with transparent
     * 
     * @param bitmap
     *            the original bitmap with a color you want to replace
     * @return a replaced color immutable bitmap
     */
    public static Bitmap createTransparentBitmapFromBitmap(Bitmap bitmap,
            int replaceThisColor) {
        if (bitmap != null) {
            int picw = bitmap.getWidth();
            int pich = bitmap.getHeight();
            int[] pix = new int[picw * pich];
            bitmap.getPixels(pix, 0, picw, 0, 0, picw, pich);

            for (int y = 0; y < pich; y++) {
                // from left to right
                for (int x = 0; x < picw; x++) {
                    int index = y * picw + x;
                    int r = (pix[index] >> 16) & 0xff;
                    int g = (pix[index] >> 8) & 0xff;
                    int b = pix[index] & 0xff;

                    if (pix[index] == replaceThisColor) {
                        pix[index] = Color.TRANSPARENT;
                    } else {
                        break;
                    }
                }

                // from right to left
                for (int x = picw - 1; x >= 0; x--) {
                    int index = y * picw + x;
                    int r = (pix[index] >> 16) & 0xff;
                    int g = (pix[index] >> 8) & 0xff;
                    int b = pix[index] & 0xff;

                    if (pix[index] == replaceThisColor) {
                        pix[index] = Color.TRANSPARENT;
                    } else {
                        break;
                    }
                }
            }

            Bitmap bm = Bitmap.createBitmap(pix, picw, pich,
                    Bitmap.Config.ARGB_4444);

            try {
                FileOutputStream out = new FileOutputStream("hehe"+ idx + ".png");
                bm.compress(Bitmap.CompressFormat.PNG, 90, out);
                idx++;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return bm;
        }
        return null;
    }
}
